<?php

namespace app\common\service\GoodsBrandService;

use app\model\BbcModel\Country;
use app\model\BbcModel\GoodsBrand;
use app\Normal;

class GoodsBrandService
{
    public function goodsBrandLists($param){
        $page = (isset($param['page']) && $param['page'] != '') ? $param['page'] : 1;
        $limit = (isset($param['limit']) && $param['limit'] != '') ? $param['limit'] : 20;
        $where = [
            ['cid','=',$param['cid']],
            ['df','=',0],
        ];
        if(isset($param['name']) && $param['name'] != ''){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        $data_lists = GoodsBrand::instance()->where($where)->limit((int)$limit)->page((int)$page)->order('sort desc,id desc')->field('id,name,pic,sort')->select();
        $total = GoodsBrand::instance()->where($where)->count();
        return Normal::returnCode(Normal::SUC, '获取成功',['data_lists'=>$data_lists,'total'=>$total]);
    }

    public function goodsBrandInfo($param){
        if(!$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少品牌ID');
        }
        $info = GoodsBrand::instance()->info(['id'=>$param['id'],'cid'=>$param['cid'],'cb_id'=>$param['cb_id'],'_fields_'=>'id,name,pic,sort,country_id']);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'品牌不存在');
        }
        return Normal::returnCode(Normal::SUC,'获取成功',$info);
    }

    public function editGoodsBrand($param){
        if(!$param['id']){
            return Normal::returnCode(Normal::FAIL,'缺少品牌ID');
        }
        if(!isset($param['name'])|| !$param['name']){
            return Normal::returnCode(Normal::FAIL,'品牌名称不能为空');
        }
        if(!isset($param['country_id'])|| !$param['country_id']){
            return Normal::returnCode(Normal::FAIL,'所属国家不能为空');
        }
        if(!Country::instance()->info(['id'=>$param['country_id']])){
            return Normal::returnCode(Normal::FAIL,'国家不存在');
        }
        $param['sort'] = $param['sort'] ?? 0;
        $param['pic'] = $param['pic'] ?? '';
        $info = GoodsBrand::instance()->info(['id'=>$param['id'],'cid'=>$param['cid'],'_fields_'=>'id,name,pic,sort,country_id']);
        if(!$info){
            return Normal::returnCode(Normal::FAIL,'品牌不存在');
        }

        $res = GoodsBrand::instance()->edit(['id'=>$param['id']],['name'=>$param['name'],'country_id'=>$param['country_id'],'sort'=>$param['sort'],'pic'=>$param['pic']]);
        if($res === false){
            return Normal::returnCode(Normal::FAIL,'编辑品牌失败');
        }
        return Normal::returnCode(Normal::SUC,'操作成功');
    }
    
    /**
     * 获取品牌下拉列表
     * @param array $param
     * @return array
     */
    public function goodsBrandListsNoPage(array $param):array{
        $where = [
            ['cid','=',$param['cid']],
            ['df','=',0],
        ];
        if(isset($param['name']) && $param['name'] != ''){
            $where[] = ['name','like',"%{$param['name']}%"];
        }
        $data_lists = GoodsBrand::instance()->where($where)->order('sort desc,id desc')->field('id,name,pic,sort')->select();
        return Normal::returnCode(Normal::SUC, '获取成功',$data_lists);
    }
}